package ioperation;

import Book.Book;
import Book.BookShelf;

import java.util.Scanner;

public class AddOperation implements IOperation{
    public void work(BookShelf bookShelf){
        System.out.println("#########增加图书#########");
        //看书架是否满了
        int currentSize=bookShelf.getUsedSize();
        if(currentSize==bookShelf.booklength()){
            System.out.println("书架满了，不能放了，下次再来吧亲亲！！！！！！！！！！");
            return;
        }

        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入书名：");
        String name=scanner.nextLine();
        System.out.println("请输入作者：");
        String author=scanner.nextLine();
        System.out.println("请输入类型：");
        String type=scanner.nextLine();
        System.out.println("请输入价格：");
        int price=scanner.nextInt();

        Book book=new Book(name,price,author,type);
        //看里面是否已经存在
        for (int i = 0; i < currentSize; i++) {
            Book book1=bookShelf.getBooks(i);
            if(book1.getName().equals(name)){
                System.out.println("书架中已经有这本书了！！！，不能再插入了");
                return;
            }
        }

        //都不成立？那就插入
        bookShelf.setBooks(book,currentSize);
        bookShelf.setUsedSize(currentSize+1);
        System.out.println("新增图书成功");

    }
}
